package logic

import (
	"context"
	"fmt"
	"wangwang/models/mysql"
	"wangwang/pkg"

	"wangwang/userrpc/internal/svc"
	"wangwang/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginUserLogic) LoginUser(in *userrpc.LoginUserRequest) (*userrpc.LoginUserResponse, error) {
	// todo: add your logic here and delete this line

	//cmd := redisinit.Client.Get("codelogin" + in.Username)
	//if in.Code != cmd.Val() {
	//	return nil, fmt.Errorf("短信验证码错误，请重新输入")
	//}
	var users mysql.Users
	mysql.Db.Debug().Where("username = ?", in.Username).Find(&users)
	if users.ID == 0 {
		return nil, fmt.Errorf("未注册的用户不允许登录")
	}
	//get := redisinit.Client.Get("captchalogin" + in.Username)
	//if in.Captcha != get.Val() {
	//	return nil, fmt.Errorf("短图形验证码错误，请重新输入")
	//}
	if users.Password != pkg.Md5Encryption(in.Password) {
		return nil, fmt.Errorf("密码不一致，请重新输入")
	}

	return &userrpc.LoginUserResponse{Id: int64(users.ID)}, nil
}
